home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
adg_1_3.zip
/
NOALPHA.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-02-21
|
13KB
|
385 lines
/****************************************************************************
Module name: NoAlpha.C
Programmer : Jeffrey M. Richter.
*****************************************************************************/
#include "..\nowindws.h"
#undef NOCOLOR
#undef NOCTLMGR
#undef NOKERNEL
#undef NOMB
#undef NOMENUS
#undef NOMSG
#undef NOSHOWWINDOW
#undef NOUSER
#undef NOWINOFFSETS
#undef NOWINMESSAGES
#undef NOWINSTYLES
#include <windows.h>
#include <stdlib.h>
#include "supercls.h"
#include "noalpha.h"
char _szAppName[] = "NoAlphaApp";
HANDLE _hInstance = NULL; // Our instance handle.
// **************************************************************************
// Message number of our message to be sent to superclass window proc.
WORD _SetRangeMsg = WM_NULL, _ValidRangeMsg = WM_NULL;
// Forward reference to various functions.
BOOL NEAR PASCAL RegisterAppWndClass (HANDLE hInstance);
BOOL NEAR PASCAL RegisterNoAlphaWndClass (HANDLE hInstance);
BOOL FAR PASCAL AboutProc (HWND, WORD, WORD, LONG);
BOOL FAR PASCAL DlgProc (HWND, WORD, WORD, LONG);
LONG FAR PASCAL NoAlphaAppWndProc (HWND, WORD, WORD, LONG);
// ***************************************************************************
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int cmdShow) {
MSG msg;
HWND hWnd;
_hInstance = hInstance;
if (hPrevInstance == NULL) { // First instance of NOALPHA running.
// Register NoAlpha's main window's class.
if (!RegisterAppWndClass(hInstance))
return(0);
// Register the NoAlpha class. This is a superclass based on the
// standard Window's "EDIT" control.
if (!RegisterNoAlphaWndClass(hInstance))
return(0);
// Register two new window messages called, "ValidRange" & "SetRange".
// If these messages have been registered before, Windows returns the
// same values that were returned the first time these messages
// were registered.
_ValidRangeMsg = RegisterWindowMessage("ValidRange");
_SetRangeMsg = RegisterWindowMessage("SetRange");
} else { // Another instance of this application is running.
// Demonstrated here are two different ways of getting the values
// of the two NoAlpha specific messages:
// Get the value associated with message, "ValidRange".
// Because this message has been previously registered, Windows
// just returns the same value that was returned the first time
// RegisterWindowMessage was called.
_ValidRangeMsg = RegisterWindowMessage("ValidRange");
// Get the value associated with the message, "SetRange".
// Because this is a global variable and another instance of this
// application is running, we can use the GetInstanceData function
// to retrieve the value for this message.
GetInstanceData(hPrevInstance, (NPSTR) &_ValidRangeMsg,
sizeof(_ValidRangeMsg));
}
// Create NoAlpha's application window.
hWnd = CreateWindow(_szAppName, _szAppName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, SW_SHOW, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, 0);
if (hWnd == NULL) return(0);
ShowWindow(hWnd, cmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(0);
}
// ***************************************************************************
// This function registers NoAlpha's application window.
BOOL near pascal RegisterAppWndClass (HANDLE hInstance) {
WNDCLASS WndClass;
WndClass.style = 0;
WndClass.lpfnWndProc = NoAlphaAppWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(hInstance, _szAppName);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = COLOR_WINDOW + 1;
WndClass.lpszMenuName = _szAppName;
WndClass.lpszClassName = _szAppName;
return(RegisterClass(&WndClass));
}
// ***************************************************************************
// This function processes all messages sent to NoAlpha's application window.
LONG FAR PASCAL NoAlphaAppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
BOOL fCallDefProc = FALSE;
LONG lResult = 0;
FARPROC fpDlgProc;
switch (wMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (wParam) {
case IDM_ABOUT:
fpDlgProc = MakeProcInstance(AboutProc, _hInstance);
DialogBox(_hInstance, "About", hWnd, fpDlgProc);
FreeProcInstance(fpDlgProc);
break;
case IDM_DIALOGBOX:
fpDlgProc = MakeProcInstance(DlgProc, _hInstance);
DialogBox(_hInstance, "DlgBox", hWnd, fpDlgProc);
FreeProcInstance(fpDlgProc);
break;
default:
break;
}
break;
default:
fCallDefProc = TRUE; break;
}
if (fCallDefProc)
lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
return(lResult);
}
// ***************************************************************************
// This function processes all messages sent to the About dialog box.
BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
char szBuffer[100];
BOOL fProcessed = TRUE;
switch (wMsg) {
case WM_INITDIALOG:
// Set version static window to have date and time of compilation.
wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
break;
case WM_COMMAND:
switch (wParam) {
case IDOK:
case IDCANCEL:
if (HIWORD(lParam) == BN_CLICKED)
EndDialog(hDlg, wParam);
break;
default:
break;
}
break;
default:
fProcessed = FALSE; break;
}
return(fProcessed);
}
// ***************************************************************************
// This function processes all messages sent to NoAlpha's dialog box.
BOOL FAR PASCAL DlgProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
BOOL fProcessed = TRUE, fInRange;
switch (wMsg) {
case WM_INITDIALOG:
// Set valid range in both NoAlpha controls.
SendDlgItemMessage(hDlg, ID_NOALPHA1, _SetRangeMsg, 0,
MAKELONG(100, 200));
SendDlgItemMessage(hDlg, ID_NOALPHA2, _SetRangeMsg, 0,
MAKELONG(-300, 100));
break;
case WM_COMMAND:
switch (wParam) {
case IDOK:
if (HIWORD(lParam) != BN_CLICKED) break;
// Send message to first NoAlpha control to see if its
// value is in range.
fInRange = (BOOL)
SendDlgItemMessage(hDlg, ID_NOALPHA1, _ValidRangeMsg, 0, 0l);
if (!fInRange) {
// Display error message to user.
MessageBox(hDlg, "Value is out of range.", _szAppName, MB_OK);
// Set focus to "NoAlpha" control so user can change its value.
SetFocus(GetDlgItem(hDlg, ID_NOALPHA1));
break;
}
// Send message to second NoAlpha control to see if its
// value is in range.
fInRange = (BOOL)
SendDlgItemMessage(hDlg, ID_NOALPHA2, _ValidRangeMsg, 0, 0l);
if (!fInRange) {
// Display error message to user.
MessageBox(hDlg, "Value is out of range.", _szAppName, MB_OK);
// Set focus to "NoAlpha" control so user can change its value.
SetFocus(GetDlgItem(hDlg, ID_NOALPHA2));
break;
}
EndDialog(hDlg, wParam);
break;
case IDCANCEL:
EndDialog(hDlg, wParam);
break;
}
default:
fProcessed = FALSE; break;
}
return(fProcessed);
}
//****************************************************************************
// Window words for the NoAlpha Super Class.
#define NOALPHA_CBCLSEXTRA (0)
#define NOALPHA_CBWNDEXTRA (4)
#define GSCWW_LOVALUE (0)
#define GSCWW_HIVALUE (2)
LONG FAR PASCAL NoAlphaWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
// ***************************************************************************
// This function registers the NoAlpha class. This is a superclassed class
// based on the standard Windows "EDIT" control.
BOOL near pascal RegisterNoAlphaWndClass (HANDLE hInstance) {
WNDCLASS WndClass;
// Retrieve current class information for "EDIT" system global class.
GetClassInfo(NULL, "EDIT", &WndClass);
// Our new class should have a new name.
WndClass.lpszClassName = "NoAlpha";
WndClass.hInstance = hInstance;
// The following WNDCLASS members are not changed for NoAlpha:
// style, hIcon, hCursor, hbrBackground, lpszMenuName.
// Register the new window class.
return(RegisterSuperClass(
&WndClass, // Address of WNDCLASS structure.
NoAlphaWndProc, // Address of superclass window procedure.
NOALPHA_CBCLSEXTRA, // Number of additional class extra bytes.
NOALPHA_CBWNDEXTRA // Number of additional window extra bytes.
));
}
// ***************************************************************************
// This function processes all messages sent to "NoAlpha" windows.
LONG FAR PASCAL NoAlphaWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
LONG lResult = 0L;
BOOL fCallBaseClassWndProc = TRUE;
char szValue[10];
int nValue, nLoValue, nHiValue;
// Check if message is the registered message: "SetRange".
if (wMsg == _SetRangeMsg) {
// Change window extra bytes to reflect new valid range.
SetSCWindowWord(hWnd, GSCWW_LOVALUE, LOWORD(lParam));
SetSCWindowWord(hWnd, GSCWW_HIVALUE, HIWORD(lParam));
// Message should not be passed to base class window procedure.
fCallBaseClassWndProc = FALSE;
}
// Check if message is the registered message: "ValidRange".
if (wMsg == _ValidRangeMsg) {
// Get value in NoAlpha window.
GetWindowText(hWnd, szValue, sizeof(szValue));
// Convert number to integer.
nValue = atoi(szValue);
// Retrieve valid range from window extra bytes.
nLoValue = GetSCWindowWord(hWnd, GSCWW_LOVALUE);
nHiValue = GetSCWindowWord(hWnd, GSCWW_HIVALUE);
// Determine if user's values is within legal range.
lResult = (nLoValue <= nValue) && (nValue <= nHiValue);
// Message should not be passed to base class window procedure.
fCallBaseClassWndProc = FALSE;
}
switch (wMsg) {
case WM_NCCREATE:
// Set the last 8 class extra bytes to have information
// about the base class.
SetSuperClassInfo(hWnd);
break;
case WM_CREATE:
// By default, set valid range to -32767 to + 32767
SetSCWindowWord(hWnd, GSCWW_LOVALUE, -32767);
SetSCWindowWord(hWnd, GSCWW_HIVALUE, +32767);
break;
case WM_CHAR:
// Prohibit message from being sent to base window procedure if
// wParam is a letter.
if (wParam >= 'A' && wParam <= 'Z') fCallBaseClassWndProc = FALSE;
if (wParam >= 'a' && wParam <= 'z') fCallBaseClassWndProc = FALSE;
// If message is a letter, notify user that key is illegal
// for a NoAlpha window by beeping.
if (fCallBaseClassWndProc == FALSE) MessageBeep(0);
break;
default: // Pass other messages to base class window procedure.
break;
}
if (fCallBaseClassWndProc) {
// Call the base class window procedure and return its result
// to the caller.
lResult =
CallWindowProc((FARPROC) GetBCWndProc(hWnd),
hWnd, wMsg, wParam, lParam);
}
return(lResult);
}